package com.temi.demo.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.temi.demo.anim.EasyTransition;
import com.temi.demo.MainActivity;
import com.temi.demo.R;
import com.temi.demo.bean.ContactsListResult;
import com.temi.demo.net.PubNubManager;
import com.temi.demo.views.CustomToolbar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by coco on 2018/6/27.
 */

public class ContactsDetailActivity extends BaseActivity {

    @BindView(R.id.activity_contacts_detail_toolbar)
    protected CustomToolbar toolbar;

    @BindView(R.id.user_icon)
    protected ImageView userIcon;

    @BindView(R.id.activity_contacts_detail_ll_content)
    protected LinearLayout llContent;

    @BindView(R.id.activity_contacts_detail_tv_name)
    protected TextView tvName;

    @BindView(R.id.activity_contacts_detail_tv_phone)
    protected TextView tvPhone;

    @BindView(R.id.activity_contacts_detail_tv_gender)
    protected TextView tvGender;

    @BindView(R.id.activity_contacts_detail_tv_email)
    protected TextView tvEmail;

    @BindView(R.id.activity_contacts_detail_tv_address)
    protected TextView tvAddr;

    @BindView(R.id.activity_contacts_detail_btn_send)
    protected Button btnSend;

    private ContactsListResult.ContactsData contactsBean;

    @Override
    public int getLayout() {
        return R.layout.activity_contacts_detail;
    }

    @Override
    protected void initTitle() {
        toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        getBundleData();
    }

    private void getBundleData() {
        Bundle bundle = getIntent().getBundleExtra(BaseActivity.KEY_BUNDLE);
        String str = bundle.getString(MainActivity.KEY_CONTACTS_DETAIL);

        contactsBean = new Gson().fromJson(str, ContactsListResult.ContactsData.class);
        if (contactsBean == null) {
            contactsBean = new ContactsListResult.ContactsData();
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        long transitionDuration = 500;
        if (null != savedInstanceState) {
            transitionDuration = 0;
        }

        EasyTransition.enter(
                this,
                transitionDuration,
                new DecelerateInterpolator(),
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        initOtherViews();
                    }
                });
    }

    private void initOtherViews() {
        llContent.setVisibility(View.VISIBLE);
        llContent.setAlpha(0);
        llContent.setTranslationY(-30);
        llContent.animate()
                .setDuration(300)
                .alpha(1)
                .translationY(0);

        btnSend.setVisibility(View.VISIBLE);
        setViews();
    }

    private void setViews() {
        String url = contactsBean.getAvatar();
        String originUrl = url.substring(0, url.indexOf("?") + 1);
        Glide.with(this)
                .load(originUrl)
                .apply(RequestOptions
                        .circleCropTransform()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(userIcon);


        String firstName = contactsBean.getFirst_name();
        String lastName = contactsBean.getLast_name();
        String name = "";
        if (!TextUtils.isEmpty(firstName)) {
            name = lastName;
        }
        if (!TextUtils.isEmpty(lastName)) {
            name += lastName;
        }
        tvName.setText(name);

        tvPhone.setText(contactsBean.getPhone());
        tvGender.setText(contactsBean.getGender());
        tvEmail.setText(contactsBean.getEmail());
        tvAddr.setText(contactsBean.getAddress());
    }

    @OnClick(R.id.activity_contacts_detail_btn_send)
    protected void sendMsg() {
        PubNubManager.getInstance().publishMessage("push notification");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);

    }

}
